using Godot;
using System;
using System.Collections.Generic;
using static Slot;

//待重写
public class StoreView : Control
{
    public BlockData block;
    public Player player;
    public String Title = "PlayerStore";
    public bool state = false;
    public AnimationPlayer ap;
    public List<Slot> slots = new List<Slot>();
    public OnClick HoldInEvent;
    public OnClick HoldOverEvent;
    public OnClick OnClickEvent;
    public OnClick OnRightClickEvent;
    public OnClick OnShiftClickEvent;

    public override void _Ready()
    {
        ap = GetNode<AnimationPlayer>("AnimationPlayer");
        Create();
    }
    public override void _Process(float delta)
    {
        
    }
    public void SetBlock(BlockData block)
    {
        this.block = block;
    }
    //初始化
    public virtual void Init(Player player)
    {
        this.player = player;
        this.OnClickEvent = player.ClickStore;
        this.OnShiftClickEvent = player.ShiftClickStore;
        this.OnRightClickEvent = player.RightClickStore;
    }
    //创建背包
    public virtual void Create()
    {
        for (int y = 0; y < 3; y++)
        {
            for (int x = 0; x < 9; x++)
            {
                slots.Add(CreateSlot(9 + y * 9 + x, 5 + x * 21, 5 + y * 21));
            }
        }

        for (int x = 0; x < 9; x++)
        {
            slots.Add(CreateSlot(x, 5 + x * 21, 77));
        }
    }
    //更新背包
    public virtual void Updata(Player player)
    {
        if (player != null)
        {
            for (int index = 0; index < slots.Count; index++)
            {
                int i = slots[index].index;
                if (i < player.store.Size)
                {
                    ItemStack item = player.store.GetItemStack(i);
                    slots[index].SetItemStack(item);
                }
                else
                {
                    slots[index].SetItemStack(null);
                }
            }
        }
    }
    //创建物品槽
    public virtual Slot CreateBlockSlot(int index, int x, int y)
    {
        PackedScene s = GD.Load<PackedScene>("res://Tscn/Slot.tscn");
        Slot slot = s.Instance<Slot>();
        slot.index = index;
        slot.HoldIn = BlockHoldIn;
        slot.HoldOver = HoldOver;
        slot.SetPosition(new Vector2(x, y));
        slot.SetClick(ClickSlot);
        slot.SetRight(RightClickSlot);
        slot.SetShiftClick(ShiftClickSlot);
        AddChild(slot);
        return slot;
    }
    //创建物品槽
    public virtual Slot CreateSlot(int index, int x, int y)
    {
        PackedScene s = GD.Load<PackedScene>("res://Tscn/Slot.tscn");
        Slot slot = s.Instance<Slot>();
        slot.index = index;
        slot.HoldIn = PlayerHoldIn;
        slot.HoldOver = HoldOver;
        slot.SetPosition(new Vector2(x, y));
        slot.SetClick(Click);
        slot.SetRight(RightClick);
        slot.SetShiftClick(ShiftClick);
        AddChild(slot);
        return slot;
    }
    public virtual void ResetShiftClick()
    {
        for (int index = 0; index < slots.Count; index++)
        {
            slots[index].SetShiftClick(ShiftClickBlockSlot);
        }
    }
    public virtual void BlockHoldIn(Slot slot)
    {
        if (HoldInEvent != null)
            HoldInEvent(slot);
        if (block != null && block.blockEntity != null)
        {
            ItemStack item = block.blockEntity.GetStore().GetItemStack(slot.index);
            if (item != null)
            {
                player.view.ItemNameTag.Visible = true;
                player.view.ItemNameTag.Text = item.DispalyName;
                player.view.Lore.Text = item.GetLore();

            }
            else
            {
                player.view.ItemNameTag.Visible = false;
                player.view.Lore.Visible = false;
            }
        }
    }
    public void PlayerHoldIn(Slot slot)
    {
        if (HoldInEvent != null)
            HoldInEvent(slot);
        if (player != null)
        {
            ItemStack item = player.store.GetItemStack(slot.index);
            if (item != null)
            {
                player.view.ItemNameTag.Visible = true;
                player.view.ItemNameTag.Text = item.DispalyName;
                player.view.Lore.Text = item.GetLore();
            }
            else
            {
                player.view.ItemNameTag.Visible = false;
                player.view.Lore.Visible = false;
            }
        }
    }
    public void HoldOver(Slot slot)
    {
        if (HoldOverEvent != null) HoldOverEvent(slot);
        if (player != null)
        {
            player.view.ItemNameTag.Text = "";
            player.view.ItemNameTag.Visible = false;
            player.view.Lore.Visible = false;
            player.view.Lore.Text = "";
        }
    }
    public void Click(Slot slot)
    {
        if (OnClickEvent != null) OnClickEvent(slot);
    }

    public void ShiftClick(Slot slot)
    {
        if (OnShiftClickEvent != null) OnShiftClickEvent(slot);
    }
    public void RightClick(Slot slot)
    {
        if (OnRightClickEvent != null) OnRightClickEvent(slot);
    }
    public void Close()
    {
        state = false;
        ap.Play("close");
        Visible = false;

        if (player != null)
        {
            player.view.ItemNameTag.Visible = false;
            player.view.Lore.Visible = false;
        }
    }
    public void Open()
    {
        state = true;
        ap.Play("open");
        Visible = true;
    }

    public StoreView GetStoreView()
    {
        PackedScene s = GD.Load<PackedScene>($"res://Tscn/StoreView.tscn");
        StoreView sv = s.Instance<StoreView>();
        return sv;
    }

    public void RightClickSlot(Slot slot)
    {
        if (block != null && block.blockEntity != null)
        {
            Store store = block.blockEntity.GetStore();
            if (BlockWorld.player.store.CursourItem != null)
            {

                if (store.MergetItem(slot.index, BlockWorld.player.store.CursourItem.Copy(1)))
                {
                    BlockWorld.player.store.SubCurosrItemAmount(1);
                }
            }
            else if (store.GetItemStack(slot.index) != null)
            {
                int amount = store.GetItemStack(slot.index).Amount;
                int halfAmount = amount / 2;
                int otherAmount = amount - halfAmount;
                if (amount == 1)
                {
                    halfAmount = 0;
                    otherAmount = 1;
                }
                BlockWorld.player.store.CursourItem = store.GetItemStack(slot.index).Copy();
                BlockWorld.player.store.CursourItem.Amount = otherAmount;
                store.SetAmount(slot.index, halfAmount);
            }
        }
    }
    public void ClickSlot(Slot slot)
    {
        if (block != null && block.blockEntity != null)
        {
            if (BlockWorld.player.store.CursourItem != null)
            {//交换和放置
                Store store = block.blockEntity.GetStore();

                if (store.GetItemStack(slot.index) == null)
                {
                    store.SetItem(slot.index, BlockWorld.player.store.CursourItem.Copy());
                    BlockWorld.player.store.CursourItem = null;
                }
                else
                {
                    ItemStack item = store.GetItemStack(slot.index);
                    if (item.equalMaterial(BlockWorld.player.store.CursourItem))
                    {
                        if (item.Amount + BlockWorld.player.store.CursourItem.Amount <= item.getMaterial().MaxAmount)
                        {
                            store.AddAmount(slot.index, BlockWorld.player.store.CursourItem.Amount);
                            BlockWorld.player.store.CursourItem = null;
                        }
                        else
                        {
                            int last = item.getMaterial().MaxAmount - item.Amount + BlockWorld.player.store.CursourItem.Amount;
                            item.Amount = item.getMaterial().MaxAmount;
                            BlockWorld.player.store.CursourItem.SetAmount(last);
                        }
                    }
                    else
                    {
                        store.SetItem(slot.index, BlockWorld.player.store.CursourItem.Copy());
                        BlockWorld.player.store.CursourItem = item;
                    }

                }
            }
            else
            {//拾取
                Store store = block.blockEntity.GetStore();
                if (store.GetItemStack(slot.index) != null)
                {
                    BlockWorld.player.store.CursourItem = store.GetItemStack(slot.index).Copy();
                    store.Remove(slot.index);
                }

            }
        }
        else
        {

        }
    }
    public void ShiftClickSlot(Slot slot)
    {
        if (block != null)
        {
            Store store = block.blockEntity.GetStore();

            if (store.GetItemStack(slot.index) != null)
            {
                BlockWorld.player.store.AddItem(store.GetItemStack(slot.index).Copy());
                store.Remove(slot.index);
            }
        }
    }
    public void ShiftClickBlockSlot(Slot slot)
    {
        if (block != null)
        {
            Store store = BlockWorld.player.store;

            if (store.GetItemStack(slot.index) != null)
            {
                block.blockEntity.GetStore().AddItem(store.GetItemStack(slot.index).Copy());
                store.Remove(slot.index);
            }
        }
    }
}
